package com.shujia.flink.window;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Duration;

/**
 * @author shujia
 */
public class Demo2EventTimeWindow {
    public static void main(String[] args) throws Exception {
        /*
         * 实时统计单词的数量，每隔5秒统计一次，统计最近5秒（滚动窗口）
         */

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        /*
         * 当任务有多个并行度时，下游算子接收到上游算子的水位线，默认取最小值，如果数据量小，会导致水位线不会向后推移
         */
        env.setParallelism(1);

        /*
java,1686966529000
java,1686966530000
java,1686966531000
java,1686966533000
java,1686966535000
java,1686966537000
java,1686966540000
         */
        //读取数据
        DataStream<String> lines = env.socketTextStream("master", 8888);

        //解析数据
        DataStream<Tuple2<String, Long>> wordAndTs = lines.map(line -> {
            String[] split = line.split(",");
            String word = split[0];
            long ts = Long.parseLong(split[1]);
            return Tuple2.of(word, ts);
        }, Types.TUPLE(Types.STRING, Types.LONG));

        //水位线和时间生成策略
        WatermarkStrategy<Tuple2<String, Long>> watermarkStrategy = WatermarkStrategy
                //指定水位线生成策略,水位线等于最新一条数据的时间戳，只能用于数据有序的情况ixa，如果数据乱序，会丢失数据
                //.<Tuple2<String, Long>>forMonotonousTimestamps()
                //指定水位线前移的时间，窗口会延迟计算，数据处理延迟会增加
                .<Tuple2<String, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                //指定时间戳字段
                .withTimestampAssigner((event, timestamp) -> event.f1);

        //告诉flink哪一个字段时时间字段
        DataStream<Tuple2<String, Long>> ass = wordAndTs
                .assignTimestampsAndWatermarks(watermarkStrategy);

        DataStream<Tuple2<String, Integer>> kvs = ass.map(kv -> {
            String word = kv.f0;
            return Tuple2.of(word, 1);
        }, Types.TUPLE(Types.STRING, Types.INT));

        KeyedStream<Tuple2<String, Integer>, String> keyBy = kvs.keyBy(kv -> kv.f0);

        //划分窗口
        //TumblingEventTimeWindows: 滚动的事件时间窗口
        WindowedStream<Tuple2<String, Integer>, String, TimeWindow> window = keyBy
                .window(TumblingEventTimeWindows.of(Time.seconds(5)));

        DataStream<Tuple2<String, Integer>> count = window.sum(1);

        count.print();

        env.execute();
    }
}
